package de.hs.rm.UJAdapter.impl;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.management.openmbean.ArrayType;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;

import org.osgi.service.upnp.UPnPStateVariable;

import de.hs.rm.UJAdapter.util.ClassTypeUtil;

public class UJActionOpenTypeGetter extends UJAction{
	
	private OpenType attributeOpenType = null;
	private String attributeName = null;
	private static final String ARRAY_PREFIX = "_"; 
	
	
	public OpenType getAttributeOpenType() {
		return attributeOpenType;
	}

	public void setAttributeOpenType(OpenType attributeOpenType) {
		this.attributeOpenType = attributeOpenType;
	}

	public String getAttributeName() {
		return attributeName;
	}

	public void setAttributeName(String attributeName) {
		this.attributeName = attributeName;
	}
	
	@Override
	public Dictionary invoke(Dictionary args) throws Exception {
		
		Dictionary returnValue = new Hashtable();
		
		Object result = mBeanServerConnection.getAttribute(objName
				, attributeName);
		ArrayList<Object> oList = new ArrayList<Object>();
		
		fillObjectListfromComplexType(result, oList);
		
		for(int i = 0; i < outputArgumentsNames.length; i++)
			returnValue.put(outputArgumentsNames[i], oList.get(i));
		
		return returnValue;
	}
	
	private void fillObjectListfromComplexType(Object openData, ArrayList<Object> oList)
	{
		//recursively add simple type state variables from complex types
		switch(ClassTypeUtil.getOpenData(openData))
		{
			case ClassTypeUtil.OPENDATA_COMPOSITE:
				CompositeDataSupport cOpenData = 
					(CompositeDataSupport) openData;
				CompositeType cType = cOpenData.getCompositeType();
				Set<String> cKeys = cType.keySet();
				Iterator<String> cIt = cKeys.iterator();
				while(cIt.hasNext())
				{
					String key = cIt.next();
					fillObjectListfromComplexType(cOpenData.get(key), oList);
				}
			break;
			
			case ClassTypeUtil.OPENDATA_ARRAY:
				Object[] aOpenData = (Object[]) openData;
				for (int i = 0; i < aOpenData.length; i++)
					fillObjectListfromComplexType(aOpenData[i], oList);
			break;
				
			case ClassTypeUtil.OPENDATA_TABULAR:
				TabularDataSupport tOpenData = 
					(TabularDataSupport) openData;
				
				Set<Entry<Object, Object>> tEntrySet=  tOpenData.entrySet();
				//Set<Object> tKeys = tOpenData.keySet();
				//Iterator<Object> kIt = tKeys.iterator();
				Iterator<Entry<Object, Object>> kIt = tEntrySet.iterator();
				while(kIt.hasNext())
				{
					Entry<Object, Object> entry = kIt.next();
					fillObjectListfromComplexType(entry.getValue(), oList);
					
				}
			break;
			
			case ClassTypeUtil.OPENDATA_SIMPLE:
				oList.add(openData);
			break;
		}
		
	}
}
